import java.io.*;
import java.net.Socket;

public class Talker extends Thread {
	private PrintWriter out;
	private String message, username;
	private Socket socket;
	private int id;

	
	public Talker(int type, String usr, String msg, String ip) { 
		username = usr;
		message = msg;
		id = type;
		if(ip.indexOf(":") > 0)
			ip = ip.split(":")[1];
		try {
			socket = new Socket(ip, 8888);
			out = new PrintWriter(socket.getOutputStream());
	
		} catch(IOException e) {
			System.out.println("Error with I/O: " + e);
		}
	}
	
	public void run() { 
		try { 
			out.println(id);
			out.println(username);
			if(message != null)
				out.println(message);
			out.flush();
			out.close();
			
			socket.close();
		} catch(IOException e) { 
			System.out.println("Error with I/O - Talker");
		}
	}
	
}
